package com.example.treeHole.service.impl;

import com.example.treeHole.dao.ArticleLikeMapper;
import com.example.treeHole.dao.NotificationMainMapper;
import com.example.treeHole.service.ArticleLikeService;
import com.example.treeHole.utils.BaseContext;
import com.example.treeHole.utils.Uuid;
import com.example.treeHole.vo.ArticleLike;
import com.example.treeHole.vo.NotificationMain;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ArticleLikeServiceImpl implements ArticleLikeService {

    @Autowired
    private ArticleLikeMapper articleLikeMapper;

    @Autowired
    private NotificationMainMapper notificationMainMapper;
    /**
     * 点赞共能实现
     * @param articleId
     */
    @Override
    public void like(String articleId) {
        String uId = BaseContext.getCurrentId();
        //先根据articleId和uId查询数据库 有则删除没有则添加
        int count=articleLikeMapper.like(articleId,uId);
        //不等于0则表示已经点赞了
        if(count!=0){
            articleLikeMapper.deleteLike(articleId,uId);
        }else {
            ArticleLike articleLike=new ArticleLike();
            articleLike.setId(Uuid.getUuID());
            articleLike.setArticleId(articleId);
            articleLike.setCreateBy(uId);
            articleLikeMapper.insert(articleLike);
            articleLike.setCreateDate(new Date());
            //todo 消息通知
            //点赞后通知文章本人，谁点赞了。
            NotificationMain notificationMain =new NotificationMain();
            notificationMain.setId(Uuid.getUuID());
            notificationMain.setArticleId(articleId);
            notificationMain.setSendId(BaseContext.getCurrentId());
            notificationMain.setCreateBy(BaseContext.getCurrentId());
            notificationMain.setCreateDate(new Date());
            //"3"表示点赞
            notificationMain.setNotificationCategory("3");
            //未处理
            notificationMain.setNotificationStatus("0");
            notificationMain.setNotificationType("0");
            notificationMainMapper.insert(notificationMain);
        }
    }
}














